<?php

//echo br(3); ###########################################################################################

	$TEMP->set_file(array('handle' => DIR_VIEW_REGISTRAR . 'studentRegistration.html'));
	
	$accountUserID = $_GET['id'];
	$warnuserpass	= "<strong>Note:</strong> If you don't want to change your password leave the password fields blank.";
	
	$departmentList = $DSC->getListDepartment();
	
	if ( isset($_POST['subButton']) ) {
		
		$if_error		= 0;
		$if_display		= 0;
		$txtcourse		= _POST('txtcourse');
		$txtaccountno	= _POST('txtaccountno');
		$txtdepartment	= _POST('txtdepartment');
		$txtfname		= _POST('txtfname');
		$txtmname		= _POST('txtmname'); 
		$txtlname		= _POST('txtlname');
		$txtgender		= _POST('txtgender');
		$txtbdatemm		= _POST('txtbdatemm');	
		$txtbdatedd		= _POST('txtbdatedd'); 
		$txtbdateyy		= _POST('txtbdateyy');
			$txtdate	= date("Y-m-d", strtotime($txtbdateyy . '-' .$txtbdatemm . '-' . $txtbdatedd));
		$txtemail		= _POST('txtemail');
		$txtcontact		= _POST('txtcontact');
		$txtaddress		= _POST('txtaddress');
		$txtguardian	= _POST('txtguardian');
		
		$txtusername	= _POST('txtusername');
		$txtpassword	= _POST('txtpassword', false);
		
		if ( empty($txtcourse) || $txtcourse == 0 )	{ $if_error++; $_txtcourse 	= REQ_FIELD; }
		if ( empty($txtaccountno) )	{ $if_error++; $_txtaccountno 				= REQ_FIELD; }
		if ( empty($txtfname) )		{ $if_error++; $_txtfname 					= REQ_FIELD; }
		if ( empty($txtlname) )		{ $if_error++; $_txtlname 					= REQ_FIELD; }
		
		if ( !empty($txtbdatemm) || !empty($txtbdatedd) || !empty($txtbdateyy) ) {
			if ( $txtdate == '1970-01-01' || $txtdate == '0000-00-00')	{ $if_error++; $_txtdate 	= INVALID_DATE; }
		}
		else {
			$txtdate == '0000-00-00';
		}
		
		if (!empty($txtemail)) {
			$emailReturn = $EMAIL->validateEmail($txtemail);
			if ($emailReturn === TRUE) {
				 $if_error++;
				$_txtemail 	= INVALID_EMAIL;
			}
		}
		
		if ( empty($accountUserID) ) {
			if ( empty($txtusername) )	{ $if_error++; $_txtusername 	= REQ_FIELD; }
			if ( empty($txtpassword) )	{ $if_error++; $_txtpassword 	= REQ_FIELD; }
		}
		
		if ( $if_error == 0 ) {
			
			$strArray = array('course_id'	=> $txtcourse,
							  'stud_no' 	=> upperCase($txtaccountno),
							  'first_name' 	=> firstUpperCase($txtfname),
							  'last_name' 	=> firstUpperCase($txtlname),
							  'middle_name' => firstUpperCase($txtmname),
							  'birthdate' 	=> ($txtdate == '1970-01-01') ? '0000-00-00' : $txtdate,
							  'gender' 		=> $txtgender,
							  'email' 		=> $txtemail,
							  'contact_no' 	=> $txtcontact,
							  'address' 	=> firstUpperCase($txtaddress),
							  'guardian' 	=> firstUpperCase($txtguardian),
							  'log_user' 	=> lowerCase($txtusername),
							  'log_pass' 	=> $txtpassword);
				
			$STUD->insertStudentInformation($strArray, true);
			$saved_record = "Saved";
			
			
		}
		else {
			if ( !empty($accountUserID) ) {
				$warning_userpass = $warnuserpass;
			}
		}
	}
	else {
		
		if ( !empty($accountUserID) ) {
			$personalInfo = $PI->getPersonalInformation($accountUserID);
			
			$txtaccountno 	= $personalInfo['account_no'];
			$txtaccess 		= $personalInfo['account_type'];
			
			$txtfname 		= $personalInfo['first_name'];
			$txtlname 		= $personalInfo['last_name'];
			$txtmname 		= $personalInfo['middle_name'];
			
			$birthdate 		= $personalInfo['birthdate'];
				$txtbdatemm	= m($birthdate);	
				$txtbdatedd	= d($birthdate); 
				$txtbdateyy	= y($birthdate);
			
			$txtgender 		= $personalInfo['gender'];
			$txtemail 		= $personalInfo['email'];
			$txtcontact 	= $personalInfo['contact_no'];
			$txtaddress 	= $personalInfo['address'];
			$txtguardian 	= $personalInfo['guardian'];
			$txtusername 	= $personalInfo['log_user'];
			$txtdepartment 	= $personalInfo['dept_idFK'];
			
			$warning_userpass = $warnuserpass;
		}
		else {
			$lastStudentID = $STUD->getLastStudentID();
			if ( empty($lastStudentID) )
				$txtaccountno 	= 'S'.date("y").'-00001';
			else {
				$nextID = $lastStudentID['stud_idPK'] + 1;
				$txtaccountno 	= 'S'.date("y").'-'.sprintf("%05d", $nextID);
			}
		}
		$txtcourse = 0;
	}
	
	$selectDepartmentList = '<select name="txtdepartment" id="txtdepartment" class="select-auto-input" onchange="courseDept();">';
		foreach($departmentList as $departmentListVal) {
			$selectedDept = ( $txtdepartment == $departmentListVal['dept_idPK'] ) ? 'selected' : '';
			$selectDepartmentList .= '<option value="' . $departmentListVal['dept_idPK'] . '" ' . $selectedDept . '>' . $departmentListVal['dept_desc'] . '</option>';
		}
	$selectDepartmentList .= '</select>';
	
	
	$TEMP->set_var(array('txtaccountno' 	=> $txtaccountno,
						 'txtcourse'		=> $txtcourse,
						 'txtfname' 		=> $txtfname,
						 'txtmname' 		=> $txtmname,
						 'txtlname' 		=> $txtlname,
						 'txtbdatedd'		=> $txtbdatedd,
						 'txtbdateyy'		=> $txtbdateyy,
						 'txtemail'			=> $txtemail,
						 'txtcontact'		=> $txtcontact,
						 'txtaddress'		=> $txtaddress,
						 'txtguardian'		=> $txtguardian,
						 'txtusername'		=> $txtusername,
						 'w_txtcourse' 		=> $_txtcourse,
						 'w_txtaccountno' 	=> $_txtaccountno,
						 'w_txtfname' 		=> $_txtfname,
						 'w_txtlname' 		=> $_txtlname,
						 'w_txtbdate'		=> $_txtdate,
						 'w_txtemail'		=> $_txtemail,
						 'w_txtusername'	=> $_txtusername,
						 'w_txtpassword'	=> $_txtpassword,
						 ));
	
	$TEMP->set_var(array('selected'.$txtaccess 	=> 'selected',
						 'selected'.$txtgender 	=> 'selected',
						 'selected'.$txtbdatemm	=> 'selected',
						 'saved_record'			=> $saved_record,
						 'udpate_user_pass'		=> $warning_userpass,
						 'department_list'		=> $selectDepartmentList,
						 'show_dept'			=> $txtdepartment,
						 'reg_sub_menu'			=> $pageLib->registrarSubMenu(DIR_IMAGES),
						 'm'					=> date("m"),
						 'd'					=> date("d"),
						 'y'					=> date("y"),
						 'h'					=> date("h"),
						 'i'					=> date("i"),
						 ));
?>